//
//  GrowProgressView.m
//  PCCBAFP
//
//  Created by MacPro on 15/6/8.
//  Copyright (c) 2015年 Puzijingrong. All rights reserved.
//

#import "PriceChartView.h"
#import <QuartzCore/QuartzCore.h>

#define KDayString @"daystring"
#define KWeekString @"weekdtring"
#define KRecordValue @"recordvalue"

@interface PriceChartView()
{
    UIImageView *_integralView;//显示当前积分的view
    UILabel *_integralLabel;//显示的积分
    NSInteger _userLeve;//用户等级
    NSUInteger _totalGrowth;//成长值
    float _floatLevel;//浮点值 用户等级
    
    NSDictionary *_growDict;//用户信息
    
    NSMutableArray *_lastSevenDayArray;
    NSMutableArray *_valueRecordArray;
    NSMutableArray *_pointRecordArray;
    
    CGFloat _maxRate;
    CGFloat _minRate;
}

@property (nonatomic, strong) NSTimer *timer;//计时器
@property CGPoint startPoint;//起始点坐标
@property CGPoint endPoint;//结束点坐标
@property CGPoint currentPoint;//当前坐标
@property float lineTop;//距离顶部
@property float lineSpace;//两条线间隔
@property float lineInterval;//五等分后的间距
@property float current;//计时器的计数


@end

@implementation PriceChartView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];//DefaultColor_BlueButton;
        _userLeve = 0;
        
        _integralView = [[UIImageView alloc] init];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"grow_progress_integral" ofType:@"png"];
        _integralView.image = [UIImage imageWithContentsOfFile:path];
        _integralView.hidden = YES;
        [self addSubview:_integralView];
        
        _integralLabel = [[UILabel alloc]  initWithFrame:CGRectMake(0, 0, 47, 30)];
        _integralLabel.backgroundColor = [UIColor clearColor];
        _integralLabel.textColor = [UIColor whiteColor];
        _integralLabel.font = Default_Bold_Font_14;
        _integralLabel.textAlignment = NSTextAlignmentCenter;
        
        [_integralView addSubview:_integralLabel];
        
        
        
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat rateSpace = (_maxRate - _minRate)/4;
    
    _lineTop = 32.0f*SCREEN_HEIGHT_RATE;//距离View的顶部
    _lineSpace = 25.0f*SCREEN_HEIGHT_RATE;//相隔多高
    _lineInterval = (self.width - (10 + 53*SCREEN_WIDTH_RATE)) / 6;
    
    CGContextSaveGState(context);
        for (int i = 1; i < 7; i++) {
                //横 实线
                CGContextBeginPath(context);
                CGContextSetLineWidth(context, 0.5f);
                CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
                CGFloat lengths[] = {5,0};
                CGContextSetLineDash(context, 0, lengths, 2);
            
            if (i == 6) {
                CGContextMoveToPoint(context, 30.0f, _lineTop + _lineSpace*i);
            }else {
                
                UILabel *rateLabel = [[UILabel alloc] initWithFrame:CGRectMake(32.0f, _lineTop + _lineSpace*i - 12.5*SCREEN_HEIGHT_RATE, 40*SCREEN_WIDTH_RATE, 25 *SCREEN_HEIGHT_RATE)];
                rateLabel.textColor = HexRGB(0x076f9e);
                
                rateLabel.text = [NSString stringWithFormat:@"%.3f",_minRate + rateSpace * (5 - i)];
                rateLabel.textAlignment = NSTextAlignmentCenter;
                rateLabel.font = Default_Font_12;
                [self addSubview:rateLabel];
                
                CGContextMoveToPoint(context, rateLabel.right + 2.0f, _lineTop + _lineSpace*i);
                
            }
            
                CGContextAddLineToPoint(context, self.width - 20,_lineTop + _lineSpace*i);
                CGContextStrokePath(context);
                
            
        }
        CGContextRestoreGState(context);
        CGContextSaveGState(context);
    
    _pointRecordArray = [NSMutableArray arrayWithCapacity:7];
    
        for (int j = 0; j < 7; j ++) {
            if (j == 0) {
                //竖 实线
                CGContextBeginPath(context);
                CGContextSetRGBStrokeColor(context, 1.0f, 1.0f, 1.0f, 0.5);//线条颜色
                
                CGFloat lengths[] = {5,0};
                CGContextSetLineDash(context, 0, lengths, 2);
                CGContextMoveToPoint(context, 30 + _lineInterval *j, _lineTop);
                CGContextAddLineToPoint(context,  30 + _lineInterval *j, _lineTop + _lineSpace*6);
                CGContextStrokePath(context);
            }
            
            CGFloat currentRate = [[_valueRecordArray objectAtIndex:j] floatValue];
            
            CGPoint point = CGPointMake(30 + _lineInterval *j, _lineTop + _lineSpace + _lineSpace * 4 *(1 - (currentRate - _minRate)/(_maxRate - _minRate)));
            [_pointRecordArray addObject:[NSValue valueWithCGPoint:point]];
            
            UILabel *vipLabel = [[UILabel alloc] initWithFrame:CGRectMake(30 + _lineInterval *j - 40*SCREEN_WIDTH_RATE / 2.0f, _lineTop + _lineSpace*6, 40*SCREEN_WIDTH_RATE, 25 *SCREEN_HEIGHT_RATE)];
            vipLabel.textColor = HexRGB(0x076f9e);
            
            NSString *dayStr = [_lastSevenDayArray objectAtIndex:j];
            dayStr = [dayStr substringFromIndex:5];
            
            vipLabel.text = [NSString stringWithFormat:@"%@",dayStr];
            vipLabel.textAlignment = NSTextAlignmentCenter;
            vipLabel.font = Default_Font_12;
            vipLabel.tag = 1001+j;
            [self addSubview:vipLabel];
            
        }
    
    CGContextRestoreGState(context);
    CGContextSaveGState(context);
    
    for (int index = 0; index < 6; index ++) {
        
        CGPoint startPoint = [[_pointRecordArray objectAtIndex:index] CGPointValue];
        CGPoint endPoint = [[_pointRecordArray objectAtIndex:index+1] CGPointValue];
        
        CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
        CGContextSetLineWidth(context, 2.0f);
        
        if (index == 5) {
            CGPoint lastPoint = CGPointMake(endPoint.x - 2, endPoint.y - 2);
            
            CGPoint aPoints[]= {startPoint,lastPoint};
            
            CGContextAddLines(context, aPoints, 2);
            CGContextDrawPath(context, kCGPathStroke);
            
            CGContextRestoreGState(context);
            CGContextSaveGState(context);
            //画圆
            CGContextBeginPath(context);
            CGRect rectToDraw = CGRectMake(endPoint.x-2, endPoint.y-2, 5, 5);
            CGContextSetLineWidth(context, 2.0f);
            
            CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
            
            CGContextStrokeEllipseInRect(context, rectToDraw);
        }else {
            
            CGPoint aPoints[]= {startPoint,endPoint};
            
            CGContextAddLines(context, aPoints, 2);
            CGContextDrawPath(context, kCGPathStroke);
        }
    }
    
    CGContextRestoreGState(context);
    CGContextSaveGState(context);
    
    CGPoint endPoint = [[_pointRecordArray lastObject] CGPointValue];
    UILabel *lastRateLabel = [[UILabel alloc] initWithFrame:CGRectMake(endPoint.x - _lineInterval/2.0f, endPoint.y - 25.0f, _lineInterval, 20.0f)];
    lastRateLabel.text = [_valueRecordArray lastObject];
    lastRateLabel.backgroundColor = [UIColor redColor];
    lastRateLabel.textAlignment = NSTextAlignmentCenter;
    lastRateLabel.font = Default_Bold_Font_12;
    lastRateLabel.clipsToBounds = YES;
    lastRateLabel.layer.cornerRadius = lastRateLabel.height/2.0f;
    [self addSubview:lastRateLabel];

}

- (void)updateGrowProgressView:(NSDictionary *)growDict
{
    _lastSevenDayArray = [self getLastNumDaysWithDate:[NSDate date] toNum:7];
    
    NSArray *array = @[@"2.699",@"2.837",@"2.801",@"2.738",@"2.705",@"2.760",@"2.691",];
    
    _valueRecordArray = [NSMutableArray arrayWithArray:array];
    
    _maxRate = 2.837;
    _minRate = 2.691;
    
    [self displayGrowProgressView];
}

//- (void)displayGrowProgressView:(NSTimer *)theTimer
- (void)displayGrowProgressView
{

    [self setNeedsDisplay];
}


-(NSMutableArray *)getLastNumDaysWithDate:(NSDate *)aDate toNum:(int)num
{
    
    NSMutableArray *array=[NSMutableArray array];
    
    //aDate给定的时间
    
    for (int i=0; i<num; i++) {
        
        NSDate *lastDay = [NSDate dateWithTimeInterval:(-24*i)*60*60 sinceDate:aDate];//前一天
        NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        NSString *lastDayStr=[formatter stringFromDate:lastDay];
        
        [array addObject:lastDayStr];
    }
    
    NSArray *dateArr=[[array reverseObjectEnumerator] allObjects];//数组倒序输出
    
    return [dateArr mutableCopy];
}

@end
